import React, { Component } from 'react'
import {RecommendedFriendsStyled} from './RecommendedFriendsStyled'

import Recommended from '../components/Recommended/Recommended'
import qs from "qs";
import { Toast } from 'antd-mobile'
import mrtx from 'images/my/denglutouxiang.png'
import { userData } from 'pages/dengluzhuce/model/userData.js'


import connect from 'pages/redux-component/connect'
let arr  = ['','','','','','','','','','','','','','','','','']
class RecommendedFriends extends Component {
  state={
    friendArr: []
  }
  render() {
    return (
      <RecommendedFriendsStyled>
        {
          this.state.friendArr.length?
          this.state.friendArr.map((value,index)=>{
            value.hdimg = 'http://laienli.cn:8080/Legang/'+value.himg
            if(value.himg){
              console.log(value.himg)
              let _arr = value.himg.split("Legang\\")
              if(_arr.length>1)
                value.hdimg = 'http://laienli.cn:8080/Legang/'+_arr[1]
            }else{
              value.hdimg = mrtx
              console.log(value.hdimg)
            }
            if(value.exp){
              if(value.exp>=500){
                value.jd = '100%'
                value.dj = 'lv5'
              }else if(value.exp>=400){
              value.jd = '80%'
              value.dj = 'lv4'
              }else if(value.exp>=300){
              value.jd = '60%'
              value.dj = 'lv3'
              }else if(value.exp>=200){
              value.jd = '40%'
              value.dj = 'lv2'
              }else{
                value.dj = 'lv1'  
                value.jd = '20%'
              }
            }else{
              value.dj = 'lv1'
              value.jd = '20%'
            }
            return <Recommended key = {index}
            data= {value}
            clickHeadImg={()=>this.onClickHeadImg(value.uid)}
            clickRecommendedInfo={()=>this.onClickRecommendedInfo(value.uid)}
            ></Recommended>
          })
          :
          arr.map((value,index)=>{
            
            return <Recommended key = {index}
            data= {{
              hdimg:mrtx,
              uname:'假数据',
              jd: '20%',
              dj: 'lv1'
            }}
            clickHeadImg={()=>this.onClickHeadImg(23)}
            clickRecommendedInfo={()=>this.onClickRecommendedInfo(1)}
            ></Recommended>
          })
        }
      </RecommendedFriendsStyled>
    );
  }
  onClickHeadImg(tag){
    console.log(tag)
  }
  onClickRecommendedInfo(tag){
    console.log(tag)
    fetch('/Legang/follow/insertFollow?fId='+tag,{   
      method:"GET"
    })    
    .then(response => response.json())
    .then(result => {
      if(result.flag){
        console.log(result)
        let arr = this.state.friendArr.filter(value=>value.uid !=tag)
        console.log(arr)
        this.setState({
          friendArr:arr
        })
      }else{
        Toast.info(result.errorMsg)
      }
      
    })
  }
  componentDidMount() {
    console.log(1)
    // let body = {uid: userData.uid}
    let body = {uid: this.props.uid}
    fetch('/Legang/follow/recommend',{   
      method:"POST",
      headers:{
          "Content-type":"application/x-www-form-urlencoded",
      },     
      body: qs.stringify(body)
    })    
    .then(response => response.json())
    .then(result => {
      if(result.flag){
        console.log(result)
        this.setState({
          friendArr : result.data
        },
        ()=>{
          console.log(this.state.friendArr)
          }
        )
      }else{
        Toast.info(result.errorMsg)
      }
      
    })
    .catch(function(e) {
      console.log("Oops, error");
    });
    
  }

}

export default connect(RecommendedFriends);